package cz.acies.utils;

import java.text.DecimalFormat;

public class Math2
{	

	public static String numberFormat(String s) throws Exception {
		String s1 = "";
		//boolean decimal = true;
		s = s.trim();
		int i = s.length();
		while (i > 0) {
			char c = s.charAt(i-1);
			if (!Character.isDigit(c)) {
				c = 0xFF;
			}
			if (c < 64) s1 = c + s1;
			i--; 
		}
		return s1;
	}

	// -----  -----------------------------------------------------------------

	public static double parseDouble(Object obj) throws Exception {
		if (obj == null) return 0.0;
		try {
			String s = obj.toString();
			s = decimalFormat(s);
			if (s.length() == 0) return 0.0;
			else {
				double d = parseDouble(s);
				return d;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0.0;
	}
	
	public static long parseBigDecimal(String s) throws Exception {
		if (s == null) return 0;
		return 0;
	}
	
	public static double parseDouble(String s) throws Exception {
		try {
			s = decimalFormat(s);
			double d = Double.parseDouble(s);
			return d;			
		} catch (Exception e) {}
		return 0.0;
	}

	public static final String NUMERIC_PATTERN = "([\\-]?)([0-9]*(?:\\.[0-9]*)?)";
	public static final String INTEGER_PATTERN = "([\\-]?)([0-9]*?)";

	public static boolean isInteger(String number) {
		if (number == null) return false;
		return number.matches(INTEGER_PATTERN);
	}

	public static boolean isNumeric(String number) {
		if (number == null) return false;
		return number.matches(NUMERIC_PATTERN);
	}

	private static String decimalFormat(String s) throws Exception {
		String s1 = "";
		boolean decimal = true;
		s = s.trim();
		int i = s.length();
		while (i > 0) {
			char c = s.charAt(i-1);
			if (c == '-') {
				if (i > 1) c = '0';	
			} else if (c == ',') {
				if (decimal) {
					decimal = false;
					if (i > 0) c = '.';
				} else {
					c = 0xFF;
				}
			} else if (c == '.') {
				if (decimal) {
					decimal = false;
				} else {
					c = 0xFF;
				}					
			} else if (!Character.isDigit(c)) {
				c = 0xFF;
			}
			if (c < 64) s1 = c + s1;
			i--; 
		}
		return s1;
	}
	
	public static String toCurrencyFormat(double value) {
		DecimalFormat formatter = new DecimalFormat("###########0.00");
		return formatter.format(value);
	}

	public static String toDecimalFormat(double value, int declen) throws Exception {
		String pattern = "###########0." + StringUtils.rfillchar("", '0', declen);
		DecimalFormat formatter = new DecimalFormat(pattern);
		return formatter.format(value);
	}

	//-----  -------------------------------------------------------------------
	
	public static double PercentMarkUp() {
		return 0.00;
	}

	/*
	 *	Vypo��ta slevu v % v pom�ru hodnot n1 a n2.
	 */
	public static double PercentDiscount(double n1, double n2) throws Exception {
		double x = 100 - ((n1/n2)*100);
		return x;
	}
	
	/*
	 *	Vypo��ta hodnotu slevy (disc) z ze zadan� hodnoty value 
	 */
	public static double discount(double value, double disc) throws Exception {
		return value - (value * (1 - (disc * 0.01)));
	}
	
	/*
	 *	Zlevn� hosnotu value o disc %
	 */
	public static double reduce(double value, double disc) throws Exception {
		return value - discount(value, disc);
	}
	
	/*
	 *	Zaokroukl� ��slo suma na po�et desetinn�ch m�st. Pokud je parametr number 
	 *	z�por� zaokrouhluje na cel� ��slo v ��du po�tu nul zadan�ch parametrem number.
	 */
	public static double roundNum(double suma,int number) throws Exception {
		int i = 0; 
		double krat = 1;
		double num = 0;

		for(i = 0; i < Math.abs(number); i++) {
			krat = krat * 10;
		}
		if(number < 0) krat = 1/krat;
		num = Math.round(suma*krat);
		num = num/krat;

		return num;
	}

	public static int stringToInt(String s) throws Exception {
		return stringToInteger(s).intValue();		
	} 	
	
	public static Integer stringToInteger(String s) throws Exception {
		try {
			if (s.indexOf(".") > 0) {
				s = s.substring(0,s.indexOf("."));
			} else if (s.indexOf(",") > 0) {
				s = s.substring(0,s.indexOf(","));
			}
			return new Integer(s);
	    } catch (Exception ex) {
	    	return new Integer("0");
	    }
	} 	
	
}
